package com.wjy.common.authentication.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjy.common.authentication.user.bean.UserAuthenticationBean;
import com.wjy.common.authentication.user.entity.UserAuthentication;
import com.wjy.common.authentication.user.mapper.UserAuthenticationMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ybxxszl
 * @since 2019-04-12
 */
@Service
public class UserAuthenticationServiceImpl extends ServiceImpl<UserAuthenticationMapper, UserAuthentication> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("username", username);
        List<UserAuthentication> userAuthenticationList = (List<UserAuthentication>) listByMap(columnMap);
        if (userAuthenticationList.size() == 0) {
            throw new UsernameNotFoundException("用户不存在");
        }
        UserAuthentication userAuthentication = userAuthenticationList.get(0);
        UserAuthenticationBean bean = new UserAuthenticationBean();
        bean.setUsername(userAuthentication.getUsername());
        bean.setPassword(userAuthentication.getPassword());
        bean.setRole(userAuthentication.getRole());
        bean.setEnabled(userAuthentication.getEnabled() != 0);
        bean.setAccountNonLocked(userAuthentication.getAccountNonLocked() != 0);
        bean.setAccountNonExpired(userAuthentication.getAccountNonExpired() != 0);
        bean.setCredentialsNonExpired(userAuthentication.getCredentialsNonExpired() != 0);
        return bean;
    }

}
